package com.itheima.health.controller;


import com.itheima.health.resource.aliyunoss.AliyunUtils;
import com.itheima.health.resource.entity.QueryPageBean;
import com.itheima.health.resource.entity.Result;
import com.itheima.health.resource.pub.MessageConstant;
import com.itheima.health.resource.pub.RedisConstant;
import com.itheima.health.resource.upload.UploadUtils;
import com.itheima.health.service.CheckGroupService;
import com.itheima.health.service.SetmealService;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.Setmeal;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;
import java.util.Set;

@RestController
@RequestMapping("setmeal")
public class SetmealController {


    @Autowired
    RedisTemplate<Object,Object> redisTemplate;  //   操作  Set

    @Reference
    private SetmealService setmealService;

    @RequestMapping("upload.do")
    public Result upload(@RequestParam("imgFile")MultipartFile file, HttpSession session){
        try {

//            String contentType = file.getContentType();
//            String originalFilename = file.getOriginalFilename();//  a.jpg
//            ServletContext servletContext = session.getServletContext();
//            String mimeType = servletContext.getMimeType(originalFilename);
//            if(mimeType=="image/jpeg"||mimeType=="image/png"){
//
//            }else{
//                throw new RuntimeException("wenjian leix cuowu ")
//            }
            //  文件上oss上 并且uudi名称 回送给前端
            String originalFilename = file.getOriginalFilename();
            String uuidfilename = UploadUtils.generateRandonFileName(originalFilename);
            //   文件上传到oss
            AliyunUtils.uploadMultiPartFileToAliyun(file.getBytes(),uuidfilename);
            //   文件存储redis   set 中
            redisTemplate.opsForSet().add(RedisConstant.SETMEAL_PIC_RESOURCES,uuidfilename);//  文件添加到redis缓存中
            //  memeber(key)
            return  new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS,uuidfilename);
        } catch (Exception e) {
            e.printStackTrace();
            return  new Result(false, MessageConstant.PIC_UPLOAD_FAIL);
        }

    }

  //  套餐的添加
    @RequestMapping("add.do")
    public Result add(@RequestBody Setmeal setmeal, @RequestParam("checkgroupIds") Integer[] checkgroupIds){
        try {
            setmealService.add(setmeal,checkgroupIds);
            return  new Result(true, MessageConstant.ADD_SETMEAL_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return  new Result(false, MessageConstant.ADD_SETMEAL_FAIL);
        }

    }
  //  套餐预约占比
    @RequestMapping("getSetmealZhanBi.do")
//    @PreAuthorize()
    public Result getSetmealZhanBi(){
        try {
           List<Map> map =  setmealService.getSetmealZhanBi();
            return  new Result(true, MessageConstant.GET_SETMEAL_COUNT_REPORT_SUCCESS,map);
        } catch (Exception e) {
            e.printStackTrace();
            return  new Result(false, MessageConstant.GET_SETMEAL_COUNT_REPORT_FAIL);
        }

    }


//    @RequestMapping("deleteCheckGroupitemById.do")
//    public Result deleteCheckGroupitemById(@RequestParam("id") Integer id){
//        try {
//
//            CheckGroup checkGroup = new CheckGroup();
//            checkGroup.setId(id);
//            checkGroup.setIs_delete(1); //  修改字段值
//            checkGroupService.updateById(checkGroup);
//            return  new Result(true, MessageConstant.UPDATE_CHECKITEM_SUCCESS);
//        } catch (Exception e) {
//            e.printStackTrace();
//            return  new Result(false, MessageConstant.UPDATE_CHECKITEM_FAIL);
//        }
//
//    }

    //  有条件和无条件查询 都是同一个方法
    @RequestMapping("findPage.do")
    public Result findPage(@RequestBody QueryPageBean queryPageBean){
        try {
            Map map = setmealService.findPageByContion(queryPageBean);
            return  new Result(true, MessageConstant.QUERY_SETMEAL_SUCCESS,map);
        } catch (Exception e) {
            e.printStackTrace();
            return  new Result(false, MessageConstant.QUERY_SETMEAL_FAIL);
        }

    }
    //  有条件和无条件查询 都是同一个方法
//    @RequestMapping("findAll")
//    public Result findAll(){
//        try {
//            QueryWrapper queryWrapper = new QueryWrapper();
//            queryWrapper.eq("is_delete",0);//  有效数据
//            List<CheckItem> list = checkItemService.list(queryWrapper);
//            return  new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS,list);
//        } catch (Exception e) {
//            e.printStackTrace();
//            return  new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
//        }
//
//    }
}
